package dz.chuxin.app.sample.douban;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import dz.chuxin.app.R;
import dz.chuxin.app.core.BaseRecyclerActivity;
import dz.chuxin.framework.widget.recycler.BaseViewHolder;
import dz.chuxin.framework.widget.recycler.ChuXinRecyclerView;
import rx.Subscriber;

/**
 * Created by chuxin on 2016/8/29.
 */
public class DouBanMovieActivity extends BaseRecyclerActivity<MovieModel> {

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        super.setUpData(savedInstanceState);
        setUpTitle("豆瓣电影Top250");
        mRecyclerView.setRefreshingOn();
    }

    @Override
    protected BaseViewHolder getRecyclerViewHolder(ViewGroup parent, int viewType) {
        return new MovieViewHolder(getLayoutInflater().inflate(R.layout.item_movie, parent, false));
    }

    @Override
    public void onRefresh(int action) {
        if (action == ChuXinRecyclerView.ACTION_PULL_TO_REFRESH) {
            mCurrentPage = 1;
            mDataList.clear();
        } else {
            mCurrentPage++;
        }
        int start = (mCurrentPage - 1) * 20;
        ApiInterface.instance().getTopMovie(new Subscriber<List<MovieModel>>() {
            @Override
            public void onCompleted() {
                mRecyclerView.onRefreshComplete();
                mRecyclerAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                mRecyclerView.onRefreshComplete();
                Logger.e(e, "网络请求异常");
            }

            @Override
            public void onNext(List<MovieModel> movieModels) {
                mDataList.addAll(movieModels);
            }
        }, bindToLifecycle(), start, 20);
    }

    class MovieViewHolder extends BaseViewHolder {

        @BindView(R.id.name_text)
        TextView nameText;
        @BindView(R.id.logo_image)
        ImageView logoImage;
        @BindView(R.id.genres_text)
        TextView genresText;
        @BindView(R.id.year_text)
        TextView yearText;
        @BindView(R.id.director_text)
        TextView directorText;
        @BindView(R.id.cast_text)
        TextView castText;

        public MovieViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void onBindViewHolder(int position) {
            MovieModel movieModel = mDataList.get(position);
            nameText.setText(movieModel.getTitle());
            Glide.with(DouBanMovieActivity.this).load(movieModel.getImages().getMedium()).centerCrop().into(logoImage);
            StringBuilder gengres = new StringBuilder();
            for (String s : movieModel.getGenresList()) {
                gengres.append(s).append(" ");
            }
            genresText.setText(String.format("标签：%s", gengres));
            yearText.setText(String.format("上映年份：%s", movieModel.getYear()));
            List<MovieModel.Cast> directors = movieModel.getDirectors();
            StringBuilder names = new StringBuilder();
            for (MovieModel.Cast director : directors) {
                names.append(director.getName()).append("，");
            }
            directorText.setText(String.format("导演：%s", names.deleteCharAt(names.length() - 1)));
            names = new StringBuilder();
            for (MovieModel.Cast cast : movieModel.getCasts()) {
                names.append(cast.getName()).append("，");
            }
            castText.setText(String.format("主演：%s", names.deleteCharAt(names.length() - 1)));
        }
    }
}
